//
//  PetServiceViewController.m
//  TaTaBang
//
//  Created by 海龙 on 15/8/16.
//  Copyright (c) 2015年 hailong. All rights reserved.
//

#import "PetServiceViewController.h"
#import "TTPetServiceCell.h"
#import "ButtomView.h"
#import "HeaderView.h"
#import "EditPetViewController.h"
#import "AddPetViewController.h"
#import "TTPetManagerAdapter.h"
#import "TTBeautyManagerAdapter.h"
#import "TTFeedServiceAdapter.h"
#import "TTTrainManagerAdapter.h"
#import "TTBookServiceAdapter.h"
#import "TTBeautyManagerSearchController.h"
#import "TTBeautyManagerEditViewController.h"
#import "TTBeautyManagerAddViewController.h"

@interface PetServiceViewController ()< ButtomViewDelegate, HeaderViewDelegate, UINavigationControllerDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) ButtomView *buttomView;
@property (nonatomic, weak) HeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *adapters;
@property (nonatomic, strong) UIView *emptyView;
@property (nonatomic, assign) UILabel *tipLabel;
@property (nonatomic, assign) HeaderViewType currentType;
@property (nonatomic, strong) NSArray *titles;
@end

@implementation PetServiceViewController

- (NSMutableArray *)adapters {
    if (!_adapters) {
        _adapters = [[NSMutableArray alloc] init];
    }
    return _adapters;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        for (NSInteger index = 0; index < 10; index++) {
            Pet *pet = [[Pet alloc] init];
            [_dataSource addObject:pet];
        }
    }
    return _dataSource;
}

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@"宠物管理", @"美容管理", @"寄养管理", @"训练管理", @"预约服务"];
    }
    return _titles;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.delegate = self;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"宠物服务";

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    
    [self setupUI];
    [self bulidTableAdapters];
    
    UIImage *originalImage = [UIImage imageNamed:@"searchBtn"];
    UIImage *image = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(complete)];
    self.navigationItem.rightBarButtonItem = rightItem;
//    if (self.dataSource.count) {
    BOOL hasData = YES;
    if (hasData) {
        [self loadTableView];
    }else {
        [self loadEmptyView];
    }

}

- (void)setupUI {
    
    NSArray *icons = @[@"tileIcon_0_0", @"tileIcon_0_1", @"tileIcon_0_2", @"tileIcon_1_0", @"tileIcon_1_1"];
    NSMutableArray<HeaderViewItem> *items = (NSMutableArray<HeaderViewItem> *)[NSMutableArray arrayWithCapacity:self.titles.count];
    for (NSInteger index = 0; index < self.titles.count; index++) {
        HeaderViewItem *item = [[HeaderViewItem alloc] init];
        item.title = self.titles[index];
        item.norIcon = icons[index];
        [items addObject:item];
    }
    HeaderView *headerView = [HeaderView headerViewWithItems:items];
    headerView.width = self.view.width;
    headerView.height = 44;
    headerView.backgroundColor = [UIColor clearColor];
    headerView.delegate = self;
    [self.view addSubview:headerView];
    self.headerView = headerView;
    
    ButtomView *buttomView = [ButtomView buttomViewWithLeftTitle:@"编辑" rightTitle:@"+ 添加"];
    buttomView.width = self.view.width;
    buttomView.height = 49;
    buttomView.y = self.view.height - buttomView.height - 64;
    buttomView.delegate = self;
    [self.view addSubview:buttomView];
    self.buttomView = buttomView;
}

- (void)bulidTableAdapters {
     //宠物管理
     TTPetManagerAdapter *petManagerAdapter = [[TTPetManagerAdapter alloc] initWithType:HeaderViewTypePetManager];
     [self.adapters addObject:petManagerAdapter];
    
     //美容管理
     TTBeautyManagerAdapter *beautyManagerAdapter = [[TTBeautyManagerAdapter alloc] initWithType:HeaderViewTypeBeautyManager];
     [self.adapters addObject:beautyManagerAdapter];
     
     //寄养管理
     TTFeedServiceAdapter *feedServiceAdapter = [[TTFeedServiceAdapter alloc] initWithType:HeaderViewTypeFeedService];
     [self.adapters addObject:feedServiceAdapter];
    
     //训练管理
     TTTrainManagerAdapter *trainManagerAdapter = [[TTTrainManagerAdapter alloc] initWithType:HeaderViewTypeTrainManager];
     [self.adapters addObject:trainManagerAdapter];
     //预约管理
     TTBookServiceAdapter *bookServiceAdapter = [[TTBookServiceAdapter alloc] initWithType:HeaderViewTypeBookService];
     [self.adapters addObject:bookServiceAdapter];
 }

- (void)loadEmptyView {
    if (_tableView && _tableView.superview) {
        _tableView.hidden = YES;
    }
    if (!_emptyView && !_emptyView.superview) {
        UIView *emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, MAINCONTENTHEIGHT-44)];
        if (!_tipLabel && !_tipLabel.superview) {
            UILabel *tipLabel = [[UILabel alloc] init];
            tipLabel.backgroundColor = [UIColor clearColor];
            tipLabel.text = @"目前添加宠物";
            tipLabel.width = SCREEN_WIDTH;
            tipLabel.height = 15;
            tipLabel.centerX = self.view.centerX;
            tipLabel.centerY = emptyView.height/2;
            tipLabel.textColor = RGBCOLOR(85, 85, 85, 1);
            tipLabel.font =  [UIFont systemFontOfSize:14];
            tipLabel.textAlignment = NSTextAlignmentCenter;
            [emptyView addSubview:tipLabel];
            self.tipLabel = tipLabel;
        }
        [self.view addSubview:emptyView];
        self.emptyView = emptyView;
    }
    _emptyView.hidden = NO;
    [self.view bringSubviewToFront:_emptyView];
}

- (void)loadTableView{
    // 先删除emptyView
    if (_emptyView && _emptyView.superview) {
        _emptyView.hidden = YES;
    }
    if (!_tableView && !_tableView.superview) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.y = CGRectGetMaxY(self.headerView.frame);
        tableView.width = self.view.width;
        tableView.height = self.view.height - 44 - 64 - 49;
        [self.view addSubview:tableView];
        self.tableView = tableView;
        self.tableView.rowHeight = 100;
    }
    _tableView.hidden = NO;
    _tableView.dataSource = self.adapters[0];
    _tableView.delegate = self.adapters[0];
    [self.view bringSubviewToFront:_tableView];
}

#pragma mark - UIButton Action
- (void)complete {
    if (self.currentType == HeaderViewTypeBeautyManager) {
        TTBeautyManagerSearchController *searchVc = [[TTBeautyManagerSearchController alloc] init];
        [self.navigationController pushViewController:searchVc animated:YES];
    }
}

#pragma mark - ButtomViewDelegate
- (void)buttomView:(ButtomView *)buttomView didSelectedButtomViewType:(ButtomViewType)type {
    UIViewController *destinationVC = nil;
    BOOL isAdd = (type == ButtomViewTypeRight);
    switch (self.currentType) {
        case HeaderViewTypePetManager:
            if (isAdd) {
                destinationVC = [[AddPetViewController alloc] init];
            }else {
                destinationVC = [[EditPetViewController alloc] init];
                [(EditPetViewController *)destinationVC setDataSource:self.dataSource];
            }
            break;
        case HeaderViewTypeBeautyManager:
            if (isAdd) {
                destinationVC = [[TTBeautyManagerAddViewController alloc] init];
            }else {
                destinationVC = [[TTBeautyManagerEditViewController alloc] init];
                [(TTBeautyManagerEditViewController *)destinationVC setDataSource:self.dataSource];
            }
            break;
        case HeaderViewTypeFeedService:
            break;
        case HeaderViewTypeTrainManager:
            break;
        case HeaderViewTypeBookService:
            break;
        default:
            break;
    }
    [self jumpToDestinationPage:destinationVC];
}

- (void)jumpToDestinationPage:(UIViewController *)destinationVC {
    if (destinationVC) {
        [self.navigationController pushViewController:destinationVC animated:YES];
    }
}

#pragma mark - HeaderViewDelegate
- (void)headerView:(HeaderView *)buttomView didSelectedButtomViewType:(HeaderViewType)type {
    self.title = self.titles[type];
    self.currentType = type;
    self.tableView.dataSource = self.adapters[type];
    self.tableView.delegate = self.adapters[type];
    [self.tableView reloadData];
//    switch (type) {
//        case HeaderViewTypePetManager:
//            NSLog(@"宠物管理");
//            break;
//        case HeaderViewTypeBeautyManager:
//            NSLog(@"美容管理");
//            break;
//        case HeaderViewTypeFeedService:
//            NSLog(@"寄养管理");
//            break;
//        case HeaderViewTypeTrainManager:
//            NSLog(@"训练管理");
//            break;
//        case HeaderViewTypeBookService:
//            NSLog(@"预约服务");
//            break;
//        default:
//            break;
//    }
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self.tableView reloadData];
}

@end
